package Server.service.impl;

import Server.service.WxPayOrderService;
import Server.tool.HttpUtil;
import Server.tool.XMLUtil;
import Server.tool.wx.PayCommonUtil;
import Server.tool.wx.WxMyConfig;
import Server.tool.wx.WxTradeType;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConstants;
import com.github.wxpay.sdk.WXPayUtil;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.jdom.JDOMException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * @author YangQ
 * @date 2020-07-03 09:59
 */
@Slf4j
@Service
public class WxPayOrderServiceImpl implements WxPayOrderService {
    @Autowired
    private WxMyConfig wxMyConfig;
    private Gson gson = new Gson();
    @Autowired
    private HttpServletRequest request;

    @Override
    public Map<String, String> h5CreatePayQrCode(String outTradeNo, String money) throws IOException, JDOMException {
        String key = wxMyConfig.getKey(); // key
        String trade_type = "MWEB";//交易类型 H5 支付
        SortedMap<Object, Object> packageParams = new TreeMap<Object, Object>();
        packageParams.put("product_id", outTradeNo);// 商品ID
        packageParams.put("appid", wxMyConfig.getAppID());//
        packageParams.put("body", "财旺商城购买");// 商品描述
        packageParams.put("out_trade_no", outTradeNo);// 商户订单号
        packageParams.put("mch_id", wxMyConfig.getMchID());//
        packageParams.put("total_fee", money);// 总金额
        packageParams.put("nonce_str", UUID.randomUUID().toString().replace("-",""));
        //H5支付要求商户在统一下单接口中上传用户真实ip地址 spbill_create_ip
        packageParams.put("spbill_create_ip", request.getHeader("X-real-ip"));// 发起人IP地址
        packageParams.put("notify_url", wxMyConfig.getNotifyUrl());// 回调地址
        packageParams.put("trade_type", trade_type);// 交易类型

        //H5支付专用
        JSONObject value = new JSONObject();
        value.put("type", "WAP");
        value.put("wap_url", "https://caiwang.shop");////WAP网站URL地址
        value.put("wap_name", "财旺商城");//WAP 网站名
        JSONObject scene_info = new JSONObject();
        scene_info.put("h5_info", value);
        packageParams.put("scene_info", scene_info.toString());

        String sign = PayCommonUtil.createSign("UTF-8", packageParams, key);
        packageParams.put("sign", sign);// 签名

        String requestXML = PayCommonUtil.getRequestXml(packageParams);
        String resXml = HttpUtil.postData("https://api.mch.weixin.qq.com/pay/unifiedorder", requestXML);
        Map map = XMLUtil.doXMLParse(resXml);
        String returnCode = (String) map.get("return_code");
        if ("SUCCESS".equals(returnCode)) {
            String resultCode = (String) map.get("result_code");
            if ("SUCCESS".equals(resultCode)) {

            } else {

            }
        } else {
            String returnMsg = (String) map.get("return_msg");
        }
        return null;
    }
}
